/* Replace "dll.h" with the name of your header */
#include "TestAL.h"
#include "al.h" 
//#include <windows.h>
#include "typeTrans.h"

JNIEXPORT int JNICALL Java_com_ikangtai_shecare_common_al_Algorithm_execAL
  (JNIEnv *env, jclass clazz, jint type, jobjectArray dayInputArray, jint dayInputLength, jobjectArray cycleInputArray, jint cycleInputLength, jobject userData, 
	jlong firstPS, jobjectArray dayOutputArray, jint dayOutputLength, jobjectArray cycleOutputArray, jint cycleOutputLength){

  	//1,get the int type
  	#ifdef JAVA_OUTPUT
	LOGW("JNI 1 type-->%d",type);fflush(stdout);
	#endif
	dayUnitDSInput* dayin = jObjectTransDayInput(env,dayInputArray,dayInputLength);
	if(dayin!=NULL){
		//testTransDayInput(dayin,dayInputLength)	;
	} 
	
	cycleUnitDS* cycle = jObjectTransCycleInput(env,cycleInputArray, cycleInputLength);
	if(cycle!=NULL){
		//testTransCycleInput(cycle,cycleInputLength);
	}

	userUnitDS user = jObjectTransUserDataInput(env,userData);
	testtransUserData(&user);
	
	dayUnitDSOutput *dayoutC = createDayOutputNum(400);
	cycleUnitDS * cycleOut = createCycleUnitNum(cycleOutputLength);
	if(dayoutC == NULL || cycleOut == NULL){
		LOGW("debuglog--C---> this is no enough space!/n");
		return ;
	}

	int dayOutLength = 0;
	int cycleOutLength = 0;
	int result = inputAlForIOS(type,dayin,dayInputLength,cycle,cycleInputLength,&user,firstPS,dayoutC,&dayOutLength,cycleOut,&cycleOutLength);
	int cycleOutTest = 0;

	(*env)->DeleteLocalRef(env, dayInputArray);
	(*env)->DeleteLocalRef(env, cycleInputArray);

	if(result == 234){
		freeDayInput(dayin);
		freeDayOutput(dayoutC);
		freeCycleUnit(cycleOut);
		freeCycleUnit(cycle);
		return 234;
	}
	
	#ifdef JAVA_OUTPUT
	LOGW("debuglog--JNI 3 inputAl has end1!/n");fflush(stdout);
	#endif

	dayOutUpdate(env,dayOutputArray, dayoutC, dayOutLength);

	#ifdef JAVA_OUTPUT
		LOGW("debuglog--JNI 3 inputAl has end2!/n");fflush(stdout);
	#endif

	cycleOutputUpdate(env,cycleOutputArray,cycleOut,cycleOutLength);

	#ifdef JAVA_OUTPUT
		LOGW("debuglog--JNI 3 inputAl has end3!/n");fflush(stdout);
	#endif

	userUnitUpdate(env,userData, user);

	#ifdef JAVA_OUTPUT
		LOGW("debuglog--JNI 3 inputAl has end4!/n");fflush(stdout);
	#endif
	freeDayInput(dayin);
	freeDayOutput(dayoutC);
	freeCycleUnit(cycleOut);
	freeCycleUnit(cycle);
	return result;
  }
